﻿using DataAccess;
using KhoaLuan.Utils;
using log4net;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Script.Serialization;
using System.Web.Services;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace KhoaLuan.MainSys.ProcessAJAX
{
    public partial class AjaxLog : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }
        [WebMethod]
        public static string GetListLog(int currentpage, int numberinpage, string keyword)
        {
            string link = "~/MainSys/Manager/Controls/ctManagementLog.ascx";
            var currentuser = UserLogin.GetUserID;
            if (!SecurityServices.HasPermison(link, currentuser, TypeAudit.View))
                return new JavaScriptSerializer().Serialize(new { Status = false, Cause = "Bạn không có quyền xem danh sách nhật ký" });
            try
            {
                tbLog_Proxy pr = new tbLog_Proxy();
                List<tbLog> list = new List<tbLog>();
                int totalitem = 0;
                if (string.IsNullOrEmpty(keyword))
                {
                    list = pr.SelectAlltbLog_Proxy();
                }
                else
                {
                    list = (pr.SelectNo_tbLog_Proxy(keyword)).ToList<tbLog>();
                }
                totalitem = list.Count;
                var list2 = list.Skip((currentpage - 1) * numberinpage).Take(numberinpage).Select(log => new
                {
                    Date = log.Date.ToString("U"),
                    log.Exception,
                    log.Id,
                    log.Level,
                    log.Logger,
                    log.Message,
                    log.Thread
                });
                return new JavaScriptSerializer().Serialize(new { Status = "Success", Data = list2, TotalItem = totalitem, CurrentPage = currentpage });
            }
            catch (Exception)
            {
                return new JavaScriptSerializer().Serialize(new { Status = "Error", Data = "" });
            }
        }
        [WebMethod]
        public static string DeleteLog(int Id)
        {
            string link = "~/MainSys/Manager/Controls/ctManagementLog.ascx";
            var currentuser = UserLogin.GetUserID;
            if (!SecurityServices.HasPermison(link, currentuser, TypeAudit.Delete))
                return new JavaScriptSerializer().Serialize(new { Status = false, Cause = "Bạn không có quyền xóa nhật ký" });
            try
            {
                tbLog_Proxy pr = new tbLog_Proxy();
                if (pr.Delete_tbLog_Proxy(Id))
                    return new JavaScriptSerializer().Serialize(new { Status = "Success" });
                return new JavaScriptSerializer().Serialize(new { Status = "Error" });
            }
            catch (Exception)
            {
                return new JavaScriptSerializer().Serialize(new { Status = "Error" });
            }
        }
        [WebMethod]
        public static string InsertLog(string Thread, string Level, string Logger, string Message, string Exception)
        {
            string link = "~/MainSys/Manager/Controls/ctManagementLog.ascx";
            var currentuser = UserLogin.GetUserID;
            if (!SecurityServices.HasPermison(link, currentuser, TypeAudit.AddNew))
                return new JavaScriptSerializer().Serialize(new { Status = false, Cause = "Bạn không có quyền thêm nhật ký" });
            try
            {
                ILog log = LogManager.GetLogger(typeof(DataAccess.tbLog).Name);
                tbLog_Proxy pr = new tbLog_Proxy();
                DataAccess.tbLog a = new DataAccess.tbLog();
                a.Id = 0;
                a.Date = DateTime.Now;
                a.Level = Level;
                a.Thread = Thread;
                a.Logger = Logger;
                a.Message = Message;
                a.Exception = Exception;
                if (pr.Insert_tbLog_Proxy(a))
                    log.Info(UserLogin.GetUserName + "Thêm mới Log thành công");
                return new JavaScriptSerializer().Serialize(new { Status = "Success" });
            }
            catch (Exception)
            {
                return new JavaScriptSerializer().Serialize(new { Status = "Error" });
            }
        }
        [WebMethod]
        public static string UpdateLog(int Id, DateTime Date, string Thread, string Level, string Logger, string Message, string Exception)
        {
            string link = "~/MainSys/Manager/Controls/ctManagementLog.ascx";
            var currentuser = UserLogin.GetUserID;
            if (!SecurityServices.HasPermison(link, currentuser, TypeAudit.Edit))
                return new JavaScriptSerializer().Serialize(new { Status = false, Cause = "Bạn không có quyền chỉnh sửa nhật ký" });
            try
            {
                ILog log = LogManager.GetLogger(typeof(DataAccess.tbLog).Name);
                tbLog_Proxy pr = new tbLog_Proxy();
                DataAccess.tbLog a = new DataAccess.tbLog();
                a.Id = Id;
                a.Date = Date;
                a.Level = Level;
                a.Thread = Thread;
                a.Logger = Logger;
                a.Message = Message;
                a.Exception = Exception;
                if (pr.Update_tbLog_Proxy(a))
                    log.Info(UserLogin.GetUserName + "Cập nhật Log thành công");
                return new JavaScriptSerializer().Serialize(new { Status = "Success" });
            }
            catch (Exception)
            {
                return new JavaScriptSerializer().Serialize(new { Status = "Error" });
            }
        }

    }
}